package com.example.generator.service.impl;

import com.example.generator.service.BaseService;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Map;

/**
 * 项目文件初始化
 *
 * @author smallNorth_Lee
 * @date 2022/1/11
 */
@Service
public class GenerationPackageInfoImpl extends BaseService {

    /**
     * @param objectMap       项目信息
     * @param projectRootPath 项目根路径
     */
    public void doGeneration(Map<String, Object> objectMap, String projectRootPath) {

        String packagePath = objectMap.get("packageName").toString().replace("\\.", "/");

        objectMap.put("packageName", packagePath + ".controller");
        super.writeFile(new File(projectRootPath + "controller/", ""), "", objectMap);

        objectMap.put("packageName", packagePath + ".entity");
        super.writeFile(new File(projectRootPath + "entity/", ""), "", objectMap);

        objectMap.put("packageName", packagePath + ".mapper.xml");
        super.writeFile(new File(projectRootPath + "mapper/xml", ""), "", objectMap);

        objectMap.put("packageName", packagePath + ".service");
        super.writeFile(new File(projectRootPath + "service/impl", ""), "", objectMap);

        objectMap.put("packageName", packagePath + ".service");
        super.writeFile(new File(projectRootPath + "service/po", ""), "", objectMap);

        objectMap.put("packageName", packagePath + ".config");
        super.writeFile(new File(projectRootPath + "config/", "Knife4jConfiguration.java"), "Knife4jConfiguration.java.ftl", objectMap);
        super.writeFile(new File(projectRootPath + "config/", "CorsConfiguration.java"), "CorsConfiguration.java.ftl", objectMap);
        super.writeFile(new File(projectRootPath + "config/", "MybatisPlusConfig.java"), "mpconfig.java.ftl", objectMap);

        objectMap.put("packageName", packagePath + ".utils");
        super.writeFile(new File(projectRootPath + "utils/", "Result.java"), "Result.java.ftl", objectMap);

        super.writeFile(new File(projectRootPath + "utils/", "BeanUtil.java"), "BeanUtil.java.ftl", objectMap);

        super.writeFile(new File(projectRootPath + "utils/", "LogAspect.java"), "LogAspect.java.ftl", objectMap);

        super.writeFile(new File(projectRootPath + "utils/", "Query.java"), "Query.java.ftl", objectMap);


        objectMap.put("packageName", packagePath + ".utils.mp");
        super.writeFile(new File(projectRootPath + "utils/mp", "BaseEntity.java"), "baseEntity.java.ftl", objectMap);
        super.writeFile(new File(projectRootPath + "utils/mp", "MyMetaObjectHandler.java"), "mpMetaHandler.java.ftl", objectMap);

        objectMap.put("packageName", packagePath + ".vo");
        super.writeFile(new File(projectRootPath + "vo/", ""), "", objectMap);

        objectMap.put("packageName", packagePath + ".application");
        super.writeFile(new File(projectRootPath + "application/", ""), "", objectMap);

        String className = objectMap.get("className") + "Application";
        super.writeFile(new File(projectRootPath, className + ".java"), "application.java.ftl", objectMap);
    }
}
